import os,sys,yaml
def read_yaml(path):
    #读取用户目录下的yaml配置文件
    with open(path, 'r',encoding='utf-8') as file:  
       return yaml.safe_load(file)
def save_yaml(path,data):
    with open(path, 'w',encoding='utf-8') as file:  
        yaml.dump(data, file, default_flow_style=False, allow_unicode=True)  
pro=os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
# print("pro",pro)
base_ser_path=os.path.join(pro,'easy_edit',"_server","flow_dir","a1","a0")
base_web_path=os.path.join(pro,'easy_edit',"_web","vue_template_store","a1","a1")
base_web_path2=os.path.join(pro,'easy_edit',"_web","vue_template_store","a1","a0")
def 获取基础组件(n,temp_id,p,a,b,c,d):
    拖拽的后端组件_vue_name=a
    拖拽的后端组件_目录1_vue_name=b
    拖拽的后端组件_目录2_vue_name=c
    拖拽的后端组件_组件_vue_name=d
    res_dct={
            "base_info": {
                "vue_name":拖拽的后端组件_vue_name,
                "index": 0,
                "deepcopy_data": {
                    "name": n
                }
            },
            "dict": {
                "0":{
                        "base_info": {
                            "vue_name":拖拽的后端组件_目录1_vue_name,
                            "index": 0,
                            "deepcopy_data": {
                            }
                        },
                        "dict": {
                        }
                }
            }

    }
    dir1_yaml=os.path.join(p,"main.yaml")
    #获取到index和目录名称
    yaml_data=read_yaml(dir1_yaml)
    # print(dir1_yaml,yaml_data)
    res_dct['dict']['0']['base_info']['index']=yaml_data['index']
    res_dct['dict']['0']['base_info']['deepcopy_data']=yaml_data
    #获取目录2数据
    dir2_lst=os.listdir(p)
    temp_id=temp_id
    for d2_name in dir2_lst:
        d2_path=os.path.join(p,d2_name)
        
        if os.path.isdir(d2_path):
            dir2_id=d2_name.replace("a","")
            d2_info={
                        "base_info": {
                            "vue_name":拖拽的后端组件_目录2_vue_name,
                            "index": 0,
                            "deepcopy_data": {
                            }
                        },
                        "dict": {
                        }

            }
            res_dct['dict']['0']['dict'][dir2_id]=d2_info
            #获取目录2配置文件
            yaml_data2=read_yaml(d2_path+os.sep+'main.yaml')
            res_dct['dict']['0']['dict'][dir2_id]['base_info']['index']=yaml_data2['index']
            res_dct['dict']['0']['dict'][dir2_id]['base_info']['deepcopy_data']=yaml_data2
            #获取组件
            temp_lst=os.listdir(d2_path)
            for t_name in temp_lst:
                
                t_path=os.path.join(d2_path,t_name)
                
                if os.path.isdir(t_path):
                    t_id=t_name.replace("a","")
                    t_info={
                                "base_info": {
                                    "vue_name":拖拽的后端组件_组件_vue_name,
                                    "index": 0,
                                    "deepcopy_data": {
                                    },
                                },
                                "dict": {
                                }

                    }
                    res_dct['dict']['0']['dict'][dir2_id]['dict'][t_id]=t_info
                    try:
                        #获取目录2配置文件
                        yaml_data3=read_yaml(t_path+os.sep+'main.yaml')
                        # print(" #获取目录2配置文件",t_path+os.sep+'main.yaml')
                        yaml_data3['version']=yaml_data3['index']#默认的组件版本 为最新的组件版本
                        res_dct['dict']['0']['dict'][dir2_id]['dict'][t_id]['base_info']['index']=yaml_data3['index']
                        #读取当前最新配置
                        yaml_data4=read_yaml(t_path+os.sep+'history'+os.sep+'a'+str(yaml_data3['index'])+os.sep+'main.yaml')
                        yaml_data3.update(yaml_data4)
                        yaml_data3["temp_id"]=temp_id+dir2_id+'_'+t_id
                        res_dct['dict']['0']['dict'][dir2_id]['dict'][t_id]['base_info']['deepcopy_data']['info']=yaml_data3
                        res_dct['dict']['0']['dict'][dir2_id]['dict'][t_id]['base_info']['deepcopy_data']['name']=yaml_data3['name']
                    except:
                        del res_dct['dict']['0']['dict'][dir2_id]['dict'][t_id]
    return res_dct

拖拽的后端组件_vue_name="a_1_0_2_18_1"
拖拽的后端组件_目录1_vue_name="a_1_0_2_19_1"
拖拽的后端组件_目录2_vue_name="a_1_0_2_20_1"
拖拽的后端组件_组件_vue_name="a_1_0_2_21_1"

拖拽的前端组件_vue_name="a_1_0_2_22_1"
拖拽的前端组件_目录1_vue_name="a_1_0_2_23_1"
拖拽的前端组件_目录2_vue_name="a_1_0_2_24_1"
拖拽的前端组件_组件_vue_name="a_1_0_2_25_1"
def 获取目录1的结构数据(类型,目录id):
    if(类型=='后端'):
        base_path=os.path.join(pro,'easy_edit',"_server","flow_dir")
        拖拽_vue_name=拖拽的后端组件_vue_name
        目录1_vue_name=拖拽的后端组件_目录1_vue_name
        目录2_vue_name=拖拽的后端组件_目录2_vue_name
        组件_vue_name=拖拽的后端组件_组件_vue_name
    elif 类型=='前端':
        base_path=os.path.join(pro,'easy_edit',"_web","vue_template_store")
        拖拽_vue_name=拖拽的前端组件_vue_name
        目录1_vue_name=拖拽的前端组件_目录1_vue_name
        目录2_vue_name=拖拽的前端组件_目录2_vue_name
        组件_vue_name=拖拽的前端组件_组件_vue_name
    a,用户id,目录1id=目录id.split("_")
    res_dct={"base_info": {
                            "vue_name":目录1_vue_name,
                            "index": 0,
                            "deepcopy_data": {
                            }
                        },
                        "dict": {
                        }}
    p=os.path.join(base_path,'a'+用户id,'a'+目录1id)
    dir1_yaml=os.path.join(p,"main.yaml")
    #获取到index和目录名称
    yaml_data=read_yaml(dir1_yaml)
    # print(dir1_yaml,yaml_data)
    res_dct['base_info']['index']=yaml_data['index']
    res_dct['base_info']['deepcopy_data']=yaml_data
    #获取目录2数据
    dir2_lst=os.listdir(p)
    temp_id=目录id+'_'
    for d2_name in dir2_lst:
        d2_path=os.path.join(p,d2_name)
        
        if os.path.isdir(d2_path):
            dir2_id=d2_name.replace("a","")
            d2_info={
                        "base_info": {
                            "vue_name":目录2_vue_name,
                            "index": 0,
                            "deepcopy_data": {
                            }
                        },
                        "dict": {
                        }

            }
            res_dct['dict'][dir2_id]=d2_info
            #获取目录2配置文件
            # print("d2_path+os.sep+'main.yaml'",d2_path+os.sep+'main.yaml')
            yaml_data2=read_yaml(d2_path+os.sep+'main.yaml')

            res_dct['dict'][dir2_id]['base_info']['index']=yaml_data2['index']
            res_dct['dict'][dir2_id]['base_info']['deepcopy_data']=yaml_data2
            #获取组件
            temp_lst=os.listdir(d2_path)
            for t_name in temp_lst:
                t_path=os.path.join(d2_path,t_name)
                
                if os.path.isdir(t_path):
                    t_id=t_name.replace("a","")
                    t_info={
                                "base_info": {
                                    "vue_name":组件_vue_name,
                                    "index": 0,
                                    "deepcopy_data": {
                                    },
                                },
                                "dict": {
                                }

                    }
                    res_dct['dict'][dir2_id]['dict'][t_id]=t_info
                    try:
                        #获取目录2配置文件
                        yaml_data3=read_yaml(t_path+os.sep+'main.yaml')
                        # print(" #获取目录2配置文件",t_path+os.sep+'main.yaml')
                        yaml_data3['version']=yaml_data3['index']#默认的组件版本 为最新的组件版本
                        res_dct['dict'][dir2_id]['dict'][t_id]['base_info']['index']=yaml_data3['index']
                        #读取当前最新配置
                        yaml_data4=read_yaml(t_path+os.sep+'history'+os.sep+'a'+str(yaml_data3['index'])+os.sep+'main.yaml')
                        yaml_data3.update(yaml_data4)
                        yaml_data3["temp_id"]=temp_id+dir2_id+'_'+t_id
                        res_dct['dict'][dir2_id]['dict'][t_id]['base_info']['deepcopy_data']['info']=yaml_data3
                        res_dct['dict'][dir2_id]['dict'][t_id]['base_info']['deepcopy_data']['name']=yaml_data3['name']
                    except:
                        del res_dct['dict'][dir2_id]['dict'][t_id]
    return res_dct
def 获取目录2的结构数据(类型,目录id):
    if(类型=='后端'):
        base_path=os.path.join(pro,'easy_edit',"_server","flow_dir")
        拖拽_vue_name=拖拽的后端组件_vue_name
        目录1_vue_name=拖拽的后端组件_目录1_vue_name
        目录2_vue_name=拖拽的后端组件_目录2_vue_name
        组件_vue_name=拖拽的后端组件_组件_vue_name
    elif 类型=='前端':
        base_path=os.path.join(pro,'easy_edit',"_web","vue_template_store")
        拖拽_vue_name=拖拽的前端组件_vue_name
        目录1_vue_name=拖拽的前端组件_目录1_vue_name
        目录2_vue_name=拖拽的前端组件_目录2_vue_name
        组件_vue_name=拖拽的前端组件_组件_vue_name
    a,用户id,目录1id,目录2id=目录id.split("_")
    res_dct={"base_info": {
                            "vue_name":目录2_vue_name,
                            "index": 0,
                            "deepcopy_data": {
                            }
                        },
                        "dict": {
                        }}
    p=os.path.join(base_path,'a'+用户id,'a'+目录1id,'a'+目录2id)
    dir1_yaml=os.path.join(p,"main.yaml")
    #获取到index和目录名称
    yaml_data=read_yaml(dir1_yaml)
    # print(dir1_yaml,yaml_data)
    res_dct['base_info']['index']=yaml_data['index']
    res_dct['base_info']['deepcopy_data']=yaml_data
    #获取组件
    temp_lst=os.listdir(p)
    for t_name in temp_lst:
        t_path=os.path.join(p,t_name)
        
        if os.path.isdir(t_path):
            t_id=t_name.replace("a","")
            t_info={
                        "base_info": {
                            "vue_name":组件_vue_name,
                            "index": 0,
                            "deepcopy_data": {
                            },
                        },
                        "dict": {
                        }

            }
            res_dct['dict'][t_id]=t_info
            try:
                #获取目录2配置文件
                yaml_data3=read_yaml(t_path+os.sep+'main.yaml')
                # print(" #获取目录2配置文件",t_path+os.sep+'main.yaml')
                yaml_data3['version']=yaml_data3['index']#默认的组件版本 为最新的组件版本
                res_dct['dict'][t_id]['base_info']['index']=yaml_data3['index']
                #读取当前最新配置
                yaml_data4=read_yaml(t_path+os.sep+'history'+os.sep+'a'+str(yaml_data3['index'])+os.sep+'main.yaml')
                yaml_data3.update(yaml_data4)
                yaml_data3["temp_id"]=目录id+'_'+t_id
                res_dct['dict'][t_id]['base_info']['deepcopy_data']['info']=yaml_data3
                res_dct['dict'][t_id]['base_info']['deepcopy_data']['name']=yaml_data3['name']
            except:
                del res_dct['dict'][t_id]
    return res_dct
def 获取组件的结构数据(类型,目录id):
    if(类型=='后端'):
        base_path=os.path.join(pro,'easy_edit',"_server","flow_dir")
        拖拽_vue_name=拖拽的后端组件_vue_name
        目录1_vue_name=拖拽的后端组件_目录1_vue_name
        目录2_vue_name=拖拽的后端组件_目录2_vue_name
        组件_vue_name=拖拽的后端组件_组件_vue_name
    elif 类型=='前端':
        base_path=os.path.join(pro,'easy_edit',"_web","vue_template_store")
        拖拽_vue_name=拖拽的前端组件_vue_name
        目录1_vue_name=拖拽的前端组件_目录1_vue_name
        目录2_vue_name=拖拽的前端组件_目录2_vue_name
        组件_vue_name=拖拽的前端组件_组件_vue_name
    a,用户id,目录1id,目录2id,组件id,组件版本=目录id.split("_")
    res_dct={"base_info": {
                            "vue_name":组件_vue_name,
                            "index": 0,
                            "deepcopy_data": {
                                "info":{},
                                "name":""
                            }
                        },
                        "dict": {
                        }}
    #读取组件配置信息
    p=os.path.join(base_path,'a'+用户id,'a'+目录1id,'a'+目录2id,'a'+组件id)
    temp_yaml=os.path.join(p,"main.yaml")
    yaml_data=read_yaml(temp_yaml)
    res_dct['base_info']["deepcopy_data"]['info']=yaml_data
    #读取组件此版本的配置信息
    yaml_data2=read_yaml(os.path.join(p,'history','a'+组件版本,'main.yaml'))
    
    yaml_data2["temp_id"]=f'a_{用户id}_{目录1id}_{目录2id}_{组件id}'
    yaml_data2['version']=组件版本
    res_dct['base_info']["deepcopy_data"]['info'].update(yaml_data2)
    res_dct['base_info']["deepcopy_data"]['name']= res_dct['base_info']["deepcopy_data"]['info']['name']
    return res_dct

def 获取后端基础组件(temp_id="a_1_0_"):
    拖拽的后端组件_vue_name="a_1_0_2_18_1"
    拖拽的后端组件_目录1_vue_name="a_1_0_2_19_1"
    拖拽的后端组件_目录2_vue_name="a_1_0_2_20_1"
    拖拽的后端组件_组件_vue_name="a_1_0_2_21_1"
    return 获取基础组件("拖拽的后端组件",temp_id,base_ser_path,拖拽的后端组件_vue_name,拖拽的后端组件_目录1_vue_name,拖拽的后端组件_目录2_vue_name,拖拽的后端组件_组件_vue_name)
def 获取前端基础组件(temp_id="a_1_1_"):
    拖拽的前端组件_vue_name="a_1_0_2_22_1"
    拖拽的前端组件_目录1_vue_name="a_1_0_2_23_1"
    拖拽的前端组件_目录2_vue_name="a_1_0_2_24_1"
    拖拽的前端组件_组件_vue_name="a_1_0_2_25_1"
    res1=获取基础组件("拖拽的前端组件",temp_id,base_web_path,拖拽的前端组件_vue_name,拖拽的前端组件_目录1_vue_name,拖拽的前端组件_目录2_vue_name,拖拽的前端组件_组件_vue_name)
    # res2=获取基础组件("拖拽的前端组件",temp_id,base_web_path2,拖拽的前端组件_vue_name,拖拽的前端组件_目录1_vue_name,拖拽的前端组件_目录2_vue_name,拖拽的前端组件_组件_vue_name)
    return res1




if __name__=='__main__':
    # print(获取后端基础组件())
    print(获取前端基础组件())
    # print(获取目录1的结构数据('后端',"a_1_1"))
    # print(获取目录2的结构数据('后端',"a_1_1_1"))
    # print(获取组件的结构数据('后端',"a_1_1_1_1_1"))